package com.atguigu.gulimall.product.service.impl;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.CategoryDao;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
        //1、查询所有分类
        List<CategoryEntity> categoryList = baseMapper.selectList(null);
        //2、组装成父子的树型结构
        //2.1)、找到一级分类
        List<CategoryEntity> c1 = categoryList.stream().filter(categoryEntity ->
             categoryEntity.getParentCid() ==0
        ).map((menu)->{
            menu.setReclassify(getReclassify(menu,categoryList));
            return menu;
        }).sorted((menu1,menu2) ->{
            return (menu1.getSort() ==null?0:menu1.getSort()) - (menu2.getSort() ==null?0:menu2.getSort());
        }).collect(Collectors.toList());

        return c1;
    }

    //递归查找所有菜单的子菜单
    private List<CategoryEntity> getReclassify(CategoryEntity entity,List<CategoryEntity> categoryList){

        List<CategoryEntity> reclassify = categoryList.stream().filter(categoryEntity ->{
            return categoryEntity.getParentCid() == entity.getCatId();
        }).map(categoryEntity -> {
            //1、找到子菜单
            categoryEntity.setReclassify(getReclassify(categoryEntity,categoryList));
            return categoryEntity;
            //菜单的排序
        }).sorted((menu1,menu2) ->{
            return (menu1.getSort() ==null?0:menu1.getSort()) - (menu2.getSort() ==null?0:menu2.getSort());
        }).collect(Collectors.toList());

        return reclassify;
    }

    @Override
    public void removeMenuByIds(List<Long> asList) {
        //TODO  1、检查当前删除的分类是否被引用

        //逻辑删除
        baseMapper.deleteBatchIds(asList);
    }

    @Override
    public Long[] findCatelogPath(Long catelogId) {
        List<Long> catelogPaths = new ArrayList<>();
        List<Long> parentPath = findParentPath(catelogId, catelogPaths);

        Collections.reverse(parentPath);
        return parentPath.toArray(new Long[parentPath.size()]);
    }

    private List<Long> findParentPath(Long catelogId,List<Long> catelogPaths){
        //1、收集当前节点id
        catelogPaths.add(catelogId);
        CategoryEntity byId = this.getById(catelogId);
        if(byId.getParentCid()!=0){
            findParentPath(byId.getParentCid(),catelogPaths);
        }
        return catelogPaths;
    }
}